home *** CD-ROM | disk | FTP | other *** search
- /* lock.c, from page 361 of Turbo C Bible */
- #include <stdio.h>
- #include <io.h>
- main ()
- {
- long curpos;
- int filehandle;
- char filename [80], buffer [80];
- printf ("Eenter name of file to test with:");
- gets (filename);
- if ((filehandle = open (filename, O_RDONLY)) == -1)
- {
- perror ("open failed");
- exit (1);
- }
- /* Read 80 characters from the file */
- if (read (filehandle, buffer, 80) == -1)
- {
- perror ("read error");
- exit (1);
- }
- /* Get and save current position */
- curpos = tell (filehandle);
- /* Now lock 80 bytes from the beginning
- of the file */
- if (lock (filehandle, OL, curpos) == -1)
- {
- perror ("locking failed");
- }
- else
- {
- printf ("First %ld bytes of file %s locked\n", curpos, filename);
- /* In an actual program, you would make
- changes and write these bytes back to
- the file before unlocking. */
- if (unlock (filehandle, 0L, curpos) == -1)
- {
- perror ("unlocking failed");
- }
- else
- {
- printf ("File unlocked\n");
- }
- }
- }